package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ContinueClassStat;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.TeacherContinueClassStat;
import com.yuyou.fn.educational.mapper.TeacherContinueClassStatMapper;
import com.yuyou.fn.educational.service.IContinueClassStatService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.ITeacherContinueClassStatService;
import com.yuyou.fn.educational.vo.RankByContinueClassVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * TeacherContinueClassStat 表数据服务层接口实现类
 *
 */
@Service
public class TeacherContinueClassStatServiceImpl extends SuperServiceImpl<TeacherContinueClassStatMapper, TeacherContinueClassStat,Long> implements ITeacherContinueClassStatService {

    @Resource
    private IContinueClassStatService continueClassStatService ;

    @Resource
    private ICourseClassService courseClassService ;

    @Override
    public void calTeacherContinueClassRate(Long areaTeamId , Long busTeamId , Long periodId, List<Long> teacherIds) {

        for(Long teacherId : teacherIds ){
            List<CourseClass> courseClassList = courseClassService.findCourseClassByPeriodIdTeacherId(periodId, teacherId);
            List<Long> classIds  = BeanUtils.toList(courseClassList,"classId");

            List<ContinueClassStat> continueClassStatList = continueClassStatService.findContinueClassStatByPeriodClassIds(periodId, classIds);

            //去掉重复的学生
            List<Long> totalStudentIds = new ArrayList<>(continueClassStatList.size() * 20 );
            List<Long> continueClassStudentIds = new ArrayList<>(continueClassStatList.size() * 20 );

            for(ContinueClassStat continueClassStat : continueClassStatList ){
                totalStudentIds.addAll(Utils.toLongIds(continueClassStat.getStudentIds()));
                continueClassStudentIds.addAll(Utils.toLongIds(continueClassStat.getContinueClassStudentIds()));
            }

            TeacherContinueClassStat teacherContinueClassStat  = new TeacherContinueClassStat();
            teacherContinueClassStat.setTotalAmount(totalStudentIds.size());
            teacherContinueClassStat.setContinueClassAmount(continueClassStudentIds.size());
            teacherContinueClassStat.setUpdateTime(TimeUtils.now());

            Example<TeacherContinueClassStat> example = TeacherContinueClassStat.createExample();
            example.createCriteria().andEqualTo("periodId" , periodId ).andEqualTo("teacherId" , teacherId );

            boolean b  = updateSelectiveByExample(teacherContinueClassStat , example );

            if(!b){
                teacherContinueClassStat.setAreaTeamId(areaTeamId);
                teacherContinueClassStat.setBusTeamId(busTeamId);
                teacherContinueClassStat.setCreateTime(TimeUtils.now());
                teacherContinueClassStat.setPeriodId(periodId);
                teacherContinueClassStat.setTeacherId(teacherId);
                insertSelective(teacherContinueClassStat);
            }


        }


    }

    @Override
    public TeacherContinueClassStat findTeacherContinueClassStat(Long periodId, Long teacherId) {
        Example<TeacherContinueClassStat> example = TeacherContinueClassStat.createExample();
        example.createCriteria().andEqualTo("periodId" , periodId ).andEqualTo("teacherId" , teacherId );
        TeacherContinueClassStat one = findOneByExample(example);
        if(one == null ){
            one = new TeacherContinueClassStat();
            one.setContinueClassAmount(0);
            one.setTotalAmount(0);
            one.setPeriodId(periodId);
            one.setTeacherId(teacherId);
        }
        return one;
    }

    @Override
    public List<RankByContinueClassVo> rank(Long areaTeamId, Long periodId) {
        return autoMapper.rank(areaTeamId,periodId );
    }

    @Override
    public RankByContinueClassVo myRank(Long areaTeamId, Long periodId,Long teacherId ) {
        return autoMapper.myRank(areaTeamId,periodId,teacherId);
    }
}